package com.codeoss.module.stock.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.stock.dao.StockInLineMapper;
import com.codeoss.module.stock.model.StockInLine;
import com.codeoss.module.stock.service.IStockInLineService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class StockInLineService extends BaseService<StockInLineMapper, StockInLine> implements IStockInLineService {
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	@Transactional
	public void insert(StockInLine stockInLine) {
		beforeSave(stockInLine, true);
		super.insert(stockInLine);
	}
	@Transactional
	public void update(StockInLine stockInLine) {
		beforeSave(stockInLine, false);
		super.update(stockInLine);
	}
	@Transactional
	protected void beforeSave(StockInLine stockInLine, boolean isNewRecord) {
		Long productId=stockInLine.getProductProductId();
	    BigDecimal qtyEntered=stockInLine.getQtyEntered();
	    Long uomUomId=stockInLine.getUomUomId();
	    if(productId!=null&&qtyEntered!=null&& uomUomId!=null) {
	    	BigDecimal movementQty=uomUomConversionService.convert(productId, uomUomId, qtyEntered);
	    	stockInLine.setMovementQty(movementQty);
	    }
	}
	
	@Override
	public List<StockInLine> getLineListByStockInId(Long stockInId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockInLine> queryWrapper = new QueryWrapper<StockInLine>();
		queryWrapper.eq("stock_in_id", stockInId);
		return list(queryWrapper);
	}
}